package com.sdcqjy.property.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.sdcqjy.mylibrary.OtherUtils;
import com.sdcqjy.mylibrary.widget.SpacesItemDecoration;
import com.sdcqjy.property.R;
import com.sdcqjy.property.adapter.ListMsgAdapter;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.presenter.ListMsgPresenter;
import com.sdcqjy.property.presenter.contract.ListMsgContract;
import com.sdcqjy.property.utils.TitleUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 磁磁帅 on 2018/3/29.
 * 功能：
 */
public class CategoryListActivity extends SimpleBaseActivity<ListMsgPresenter> implements ListMsgContract.View,
        ListMsgAdapter.OnAdapterClickListener {
    @BindView(R.id.textTitle)
    TextView textTitle;
    @BindView(R.id.listView)
    RecyclerView listView;
    private int categoryID;
    private List<ListMsgMode> modeList;
    private ListMsgAdapter adapter;

    public static void open(Context context, int categoryID) {
        Intent intent = new Intent(context, CategoryListActivity.class);
        intent.putExtra("categoryID", categoryID);
        context.startActivity(intent);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_msg);
        ButterKnife.bind(this);

        presenter = new ListMsgPresenter(this);

        Intent intent = getIntent();
        categoryID = intent.getIntExtra("categoryID", 0);

        textTitle.setText(TitleUtils.getTitle(categoryID));
        modeList = new ArrayList<>();
        adapter = new ListMsgAdapter(getActivity(), modeList).setOnAdapterClickListener(this);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //int line = getResources().getColor(R.color.line);
        listView.addItemDecoration(new SpacesItemDecoration(OtherUtils.dip2px(getActivity(), 10), new boolean[]{false, false, false, true}));
    }

    protected void asynRun() {
        presenter.getList(categoryID);
    }

    public void onAdapterClicked(ListMsgMode mode) {
        DetailsActivity.open(getActivity(), mode);
//        if (TitleUtils.isEmpty(mode.imagePath)) {
//            HtmlActivity.open(getActivity(), mode);
//        } else ImageActivity.open(getActivity(), mode);
        //WebViewActivity.open(getActivity(), mode.title, mode.htmlPath);
        //"file:///android_asset/1496717486420.html");
        //WordActivity.open(getActivity());
    }

    public void getListRet(List<ListMsgMode> list) {
        modeList.clear();
        modeList.addAll(list);
        for (ListMsgMode mode : list) {
            mode.categoryID = categoryID;
        }
        adapter.notifyDataSetChanged();
    }
}
